<?
    /**
     * Smarty plugin
     * @package Smarty
     * @subpackage plugins
     */


    /**
     * Smarty {load_newsitems} function plugin
     *
     * Type:     function
     * Name:     load_newsitems
     * Purpose:  Loads most recent listings from the database
     *   @param array
     *   @param Smarty
     *
     * This smarty plugin can be talked to using the following
     * template parameters:
     *   
     *   * var (string)   -- name of variable where to load the 
     *                       data to
     *                       >> default: listings
     *   * items (string) -- total number of listing to get from
     *                       the database
     *                       >> default: 5
     *
     * @return string
     */
    function smarty_function_load_pages($params, &$smarty)
    {
        global $db; // create a link to the database connection

        //pre-process parameters
        //variable name
        if(empty($params['var'])){
            $var = 'pages';
        }else{
            $var = $params['var'];
        }
        
        //do we want to limit to some specific folder structure?
        if(empty($params['folder'])){
            $folder = null;
        }else{
            $node  = $db->getRow("SELECT * FROM structure WHERE id = ".(int)$params['folder']);
            $nodes = $db->getCol("SELECT id FROM structure WHERE lft >= ".(int)$node['lft']." AND rgt <= ".(int)$node['rgt']);
            if(!empty($nodes)){
                $folder = ' AND pm.nodeid IN('.implode(',',$nodes).') ';
            }
        }
                
        //how do I order by?
        if(empty($params['order'])){
            $order = 'pm.published';
        }else{
            $order = $params['order'];
        }
        
        //variable name
        if(empty($params['items'])){
            $total = 3;
        }else{
            $total = (int)$params['items'];
        }
        
        //pagetype
        $pageType = (int)$db->getOne("SELECT id FROM datatype WHERE lower(codeset) = 'pageadv'");
        
        
        //$filePath = ROOT.'files'.DIRECTORY_SEPARATOR.'pages'.DIRECTORY_SEPARATOR.$pageType.'__'.$this->object.DIRECTORY_SEPARATOR;
        
        //get listings
        if($total == 1){
            $smarty->assign($var,$db->getRow("SELECT 
                                                pm.created          AS intime,
                                                pm.title            AS title,
                                                ni.teaser           AS teaser,
                                                ni.content          AS content,
                                                ni.id               AS id,
                                                get_path(pm.nodeid)||pm.pageurl||'.html' AS url
                                            FROM pagemap AS pm 
                                            LEFT JOIN node_page_full AS ni ON pm.pageid = ni.id
                                            WHERE pm.pagetype = " . $pageType . " " . $folder . " " . $image . "
                                            AND pm.wf_state = 1
                                            ORDER BY ".$order." DESC LIMIT ".$total));
        }else{
            $smarty->assign($var,$db->getAll("SELECT 
                                                pm.created          AS intime,
                                                pm.title            AS title,
                                                ni.teaser           AS teaser,
                                                ni.content          AS content,
                                                ni.id               AS id,
                                                get_path(pm.nodeid)||pm.pageurl||'.html' AS url
                                            FROM pagemap AS pm 
                                            LEFT JOIN node_page_full AS ni ON pm.pageid = ni.id
                                            WHERE pm.pagetype = " . $pageType . " " . $folder . " " . $image . "
                                            AND pm.wf_state = 1
                                            ORDER BY ".$order." DESC LIMIT ".$total));
        }
    }
?>
